home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5966 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: newshost.cyberramp.net!news
  2. From: sinan@cyberramp.net (John Noland)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Do you have ever pass structures?
  5. Date: 22 Feb 1996 04:38:51 GMT
  6. Organization: Prose Software
  7. Message-ID: <4ggs0r$2bt@newshost.cyberramp.net>
  8. References: <4ge8mi$qjm@srvr1.engin.umich.edu>
  9. NNTP-Posting-Host: ramp1-27.cyberramp.net
  10. X-Newsreader: WinVN 0.99.5
  11.  
  12. In article <4ge8mi$qjm@srvr1.engin.umich.edu>, hasdi@news-server.engin.umich.edu says...
  13. >
  14. >snip...
  15. >So is there any REAL advantage is passing an entire structure? Do people
  16. >ever do it? I've some people's source code and almost always, they
  17. >pass pointers to structures instead of the structure itself. In a way,
  18. >passing a pointer to an array instead of the array itself might be
  19. >feature not a bug in C. :)
  20.  
  21. If you look at the original K&R, the only things you could do with a 
  22. structure were take its address with &, and access one of it's members.
  23. The "C Reference Manual"[RefMan81 - Bell Labs] introduced new operations
  24. on strucures and unions. It allowed them to, among other things, be passed
  25. as parameters and return types. These days the guidelines you should follow
  26. are laid out in the ANSI C standard and K&R, 2nd edition. If you don't have 
  27. the 2nd edition of K&R. Get it. I would recommend you read it a few times 
  28. and then keep it close at hand when you're programming. I know this doesn't
  29. really answer your question directly, but if you're going to be a good 
  30. programmer you have to read a lot of books. If you're going to be a good
  31. C programmer, it helps if one of those books is K&R. 
  32.  
  33. Anyway, I'll give you this much (but you should still read K&R), C passes 
  34. all arguments except arrays by value - a copy of a variable is passed to
  35. a function on the stack. (You could argue - correctly - that arrays are 
  36. not really an exception because C passes the pointer to the first element
  37. by value, but that isn't the way it's usually presented). If you have a 
  38. structure of any size, do you want it passed in it's entirety on the 
  39. stack? Or would you prefer just a pointer on the stack? It's entirely
  40. to your discretion. For what it's worth, in my experience, I have seldom 
  41. run across a circumstance where I have felt the need to pass a structure 
  42. instead of a pointer. 
  43.  
  44. - John
  45.  
  46.  
  47.